//
//  XMTabBarButton.m
//  XMWeibo
//
//  Created by chi on 14-8-19.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMTabBarButton.h"
#import "XMBadgeView.h"

#define kImageHeight 20.0
#define kImageTopEdge 5.0


@interface XMTabBarButton ()

@property (nonatomic, weak) XMBadgeView *badgeView;

@end

@implementation XMTabBarButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        if (!isIOS7) {
            [self setBackgroundImage:[UIImage adaptIOS7ImageWithName:@"tabbar_slider"] forState:UIControlStateSelected];
            [self setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        }
        else {
            [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        
        [self setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        
        //设置title和image
        self.imageView.contentMode = UIViewContentModeCenter;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:11];
        
        //创建badge
        [self setupBadge];
    }
    return self;
}

- (void)setupBadge
{
    XMBadgeView *view = [XMBadgeView buttonWithType:UIButtonTypeCustom];
    self.badgeView = view;
    

    
    [self addSubview:view];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    CGRect badgeFrame = self.badgeView.frame;
    CGFloat badgeX = frame.size.width * 0.7;
    badgeFrame.origin.x = badgeX;
    badgeFrame.origin.y = 10.0;
    self.badgeView.frame = badgeFrame;
    
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageX = 0.0;
    CGFloat imageY = kImageTopEdge;
    CGFloat imageW = contentRect.size.width;
    CGFloat imageH = kImageHeight;
    
    return CGRectMake(imageX, imageY, imageW, imageH);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleX = 0.0;
    CGFloat titleY = kImageHeight + kImageTopEdge;
    CGFloat titleW = contentRect.size.width;
    CGFloat titleH = contentRect.size.height - kImageHeight;
    
    return CGRectMake(titleX, titleY, titleW, titleH);
}

- (void)setTabBarItem:(UITabBarItem *)tabBarItem
{
    _tabBarItem = tabBarItem;
    

 
    //KVO
    [_tabBarItem addObserver:self forKeyPath:@"badgeValue" options:NSKeyValueObservingOptionOld context:nil];
    [_tabBarItem addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionOld context:nil];
    [_tabBarItem addObserver:self forKeyPath:@"selectedImage" options:NSKeyValueObservingOptionOld context:nil];
    [_tabBarItem addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionOld context:nil];
    
    
    [self observeValueForKeyPath:@"all" ofObject:nil change:nil context:nil];
    
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{

    if ([keyPath isEqualToString:@"all"]) {
        [self setImage:_tabBarItem.image forState:UIControlStateNormal];
        [self setImage:_tabBarItem.selectedImage forState:UIControlStateSelected];
        [self setTitle:_tabBarItem.title forState:UIControlStateNormal];
//        [self.badgeView setBadgeValue:_tabBarItem.badgeValue];
//        
//        CGRect frame = self.badgeView.frame;
//        frame.origin.x = -frame.size.width - 5;
//        frame.origin.y = 2.0;
//        self.badgeView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
//        self.badgeView.frame = frame;
    }
    else if ([keyPath isEqualToString:@"badgeValue"]) {
        [self.badgeView setBadgeValue:_tabBarItem.badgeValue];

//        self.badgeView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;

    }
    else if ([keyPath isEqualToString:@"title"]) {
        [self setTitle:_tabBarItem.title forState:UIControlStateNormal];
    }
    else if ([keyPath isEqualToString:@"image"]) {
        [self setImage:_tabBarItem.image forState:UIControlStateNormal];
    }
    else if ([keyPath isEqualToString:@"selectedImage"]) {
        [self setImage:_tabBarItem.selectedImage forState:UIControlStateSelected];
    }
    
    
}

- (void)setHighlighted:(BOOL)highlighted
{
    
}


@end
